package com.cxq;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by cxq on 2020/7/26.
 */
public class ThreadProblem {
//    private static int n;//执行n++操作的变量

    private static AtomicInteger atomicInteger;
    public static void main(String[] args) throws InterruptedException {

        for (int j = 0; j < 100000; j++) {
//            n=0;
            atomicInteger=new AtomicInteger(0);
            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 1000; i++) {
//                        n++;
                        atomicInteger.getAndIncrement();
                    }
                }
            });
            Thread thread2 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 1000; i++) {
//                        n++;
                        atomicInteger.getAndIncrement();
                    }
                }
            });
            Thread thread3 = new Thread(new Runnable() {
                public void run() {
                    for (int i = 0; i < 1000; i++) {
//                        n++;
                        atomicInteger.getAndIncrement();
                    }
                }
            });
            thread1.start();
            thread2.start();
            thread3.start();
            thread1.join();
            thread2.join();
            thread3.join();
            System.out.println(atomicInteger.get());

        }
    }
}
